// -----------------------------------------------------------------------
// <copyright file="Helpers.cs" company="Imar.Spaanjaars.Com">
//   Copyright 2008 - 2009 - Imar.Spaanjaars.Com. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI.WebControls;
using Resources;
using Spaanjaars.ContactManager.BusinessEntities;

/// <summary>
/// Helper class with methods used throughout the site.
/// </summary>
public static class Helpers
{
	/// <summary>
	/// Gets a localized text by its key.
	/// </summary>
	/// <param name="k">The key for the localized resource.</param>
	public static string GetTranslation(object k)
	{
		return General.ResourceManager.GetString(k.ToString());
	}

	/// <summary>
	/// Gets a list of items from the ContactType enum for a DropDownList.
	/// </summary>
	public static List<ListItem> GetContactTypes()
	{
		return GeEnumMembers(typeof(ContactType));
	}

	/// <summary>
	/// Gets a list of items from the PersonType enum for a DropDownList.
	/// </summary>
	public static List<ListItem> GetPersonTypes()
	{
		return GeEnumMembers(typeof(PersonType));
	}

	/// <summary>
	/// Helper method to get items from an enum.
	/// Code inspired by http://www.codeproject.com/cs/miscctrl/enumedit.asp
	/// </summary>
	/// <param name="theType">The type of the enum for which the items are retrieved.</param>
	private static List<ListItem> GeEnumMembers(Type theType)
	{
		List<ListItem> myList = new List<ListItem>();
		FieldInfo[] myEnumFields = theType.GetFields();
		foreach (FieldInfo myField in myEnumFields)
		{
			if (!myField.IsSpecialName && myField.Name.ToLower() != "none")
			{
				string locallizedField = General.ResourceManager.GetString(myField.Name);
				myList.Add(new ListItem(locallizedField, myField.Name));
			}
		}
		return myList;
	}
}
